package value

import (
	"context"
	"tongquetai_server/activity_vk/domain/manage/common/static_data"
	"tongquetai_server/activity_vk/infrastructure/utils"
)

// 铜雀台
const (
	CashCurrencyTypeV2           = "现金"                     // 币种类型，现金
	CashAndVoucherCurrencyTypeV2 = "现金和点券"                  // 币种类型，现金和点券
	VoucherCurrencyTypeV2        = "点券"                     // 币种类型，点券
	FoodCurrencyTypeV2           = "食物"                     // 币种类型，食物
	GoldCoinCurrencyTypeV2       = "金币"                     // 币种类型，金币
	CashItemIdV2                 = "cash_item_id"           // 道具Id，现金
	FoodItemIdV2                 = "role_attr_4"            // 道具Id，食物
	CurrencyItemIdV2             = "virtual_currency_1"     // 道具Id，金币
	CouponItemIdV2               = "activity_item_1052401"  // 道具Id，点券
	ScrapGoldIdV2                = "activity_item_10920501" // 道具Id，碎金
)

// 服务端
const (
	SvrGameCoin   = 1
	SvrCash       = 2
	SvrCoinOrCash = 3
)
// @META.table:json
/*
[
    ["活动商店配置"],
    ["编号", "货币种类", "货币数量", "购买项id", "价格", "限购数量", "是否每日重置", "奖励内容"],
    [1, "食物", 1, "", "", 1, "是", "金币*1"],
    [2, "金币", 1, "", "", 1, "是", "金币*1"]
]
*/
// TqtActivityStoreContentV2 商店配置
// @META.struct:tqt
type TqtActivityStoreContentV2 struct {
	No               int      `json:"no" header:"编号" validate:"gt=0"`
	Currency         string   `json:"currency" header:"货币种类" validate:"required"`
	CurrencyCount    int      `json:"currencyCount" header:"货币数量" allowEmpty:"true" validate:"gte=0"`
	LogicalProductId string   `json:"logicalProductId" header:"购买项id" allowEmpty:"true"`
	Price            float64  `json:"price" header:"价格" allowEmpty:"true" validate:"gte=0"`
	BuyCountLimit    int      `json:"buyCountLimit" header:"限购数量" validate:"gt=0"`
	IsDailyReset     bool     `json:"isDailyReset" header:"是否每日重置" true:"是" false:"否"`
	Award            TqtAward `json:"award" header:"奖励内容" validate:"gt=0,dive"`
}
// @META.struct:svr
type SvrActivityStoreContentV2 struct {
	No            int      `json:"no"`
	Price         SvrAward `json:"price"`
	BuyCountLimit int      `json:"buy_count_limit"`
	IsDailyReset  bool     `json:"is_daily_reset"`
	Award         SvrAward `json:"award"`
	PayType       int      `json:"pay_type"` // v2 新增
}

// ------------------------------------ 适配器部分 -----------------------------------

/*
商店配置
1. 当货币种类为"现金"时，服务端的price为空道具对象且"count"为0
2. 当货币种类不是"现金"时，服务端的price字段"count"取"货币数量"
*/

// ToTqtActivityStoreContentV2 转铜雀台
// actType: 活动类型 useSystem: 静态表礼包需要过滤的参数
func ToTqtActivityStoreContentV2(ctx context.Context, svrActivityStore []SvrActivityStoreContentV2,
	actType int, useSystem string) (tqtActivityStore []TqtActivityStoreContentV2, err error) {
	// 购买项
	productIdMap, err := static_data.GetPayLogicalProduct().GetProductIdMap(ctx)
	if err != nil {
		return
	}
	// 礼包静态表
	packageGameObjectIdMap, err := static_data.GetGiftPackage(actType, useSystem).GetGameObjectIdMap(ctx)
	if err != nil {
		return
	}
	// 初始化
	var (
		productItem      static_data.PayLogicalProduct
		logicalProductId string
		price            float64
		tqtPriceAward    TqtAward
		currency         string
		currencyItemId   string
	)
	tqtActivityStore = make([]TqtActivityStoreContentV2, 0, len(svrActivityStore))
	for _, item := range svrActivityStore {
		tqtPriceAward = SvrToTqtAward(ctx, item.Price)
		// 现金不是道具，需要特殊处理
		currency = tqtPriceAward.ItemName
		currencyItemId = tqtPriceAward.ItemId
		logicalProductId = ""
		price = 0
		if currency == "" {
			currency = CashCurrencyTypeV2
			currencyItemId = CashItemIdV2
			productItem = getProductInfo(item.Award.GameObjectId, productIdMap, packageGameObjectIdMap)
			logicalProductId = productItem.Id
			price = productItem.Price
		}
		// 《现金和点券》不是道具 需要特殊处理
		if currencyItemId == CouponItemIdV2 && item.PayType == SvrCoinOrCash {
			currency = CashAndVoucherCurrencyTypeV2
			currencyItemId = CouponItemIdV2
			productItem = getProductInfo(item.Award.GameObjectId, productIdMap, packageGameObjectIdMap)
			logicalProductId = productItem.Id
			price = productItem.Price
		}

		tqtActivityStore = append(tqtActivityStore, TqtActivityStoreContentV2{
			No:               item.No,
			Currency:         currency,
			CurrencyCount:    tqtPriceAward.ItemCount,
			LogicalProductId: logicalProductId, // 购买项转换，货币类型为现金时，购买项必填
			Price:            price,            // 购买项转换，货币类型为现金时，价格必填
			BuyCountLimit:    item.BuyCountLimit,
			IsDailyReset:     item.IsDailyReset,
			Award:            SvrToTqtAward(ctx, item.Award),
		})
	}
	return
}

// ToSvrActivityStoreContentV2 转服务端
func ToSvrActivityStoreContentV2(ctx context.Context,
	tqtActivityStore []TqtActivityStoreContentV2) (svrActivityStore []SvrActivityStoreContentV2, err error) {
	// 币种类型道具
	var (
		itemInfo        utils.ItemInfo
		tqtCurrencyItem TqtItem
		currencyCount   int
		payType         int
	)
	svrActivityStore = make([]SvrActivityStoreContentV2, 0, len(tqtActivityStore))
	for _, v := range tqtActivityStore {
		// 获取相应的数据
		currencyCount = v.CurrencyCount
		// 现金时,数据为空
		if v.Currency == CashCurrencyTypeV2 {
			tqtCurrencyItem = TqtItem{}
			currencyCount = 0
			payType = SvrCash
		} else if v.Currency == CashAndVoucherCurrencyTypeV2 {
			// 现金和点券 gid填点券 pay_type为3
			payType = SvrCoinOrCash
			itemName := utils.GetItemNameByItemId(ctx, CouponItemIdV2)
			tqtCurrencyItem = TqtItem{
				ItemId:   CouponItemIdV2,
				ItemName: itemName,
			}
		} else {
			itemInfo, err = utils.GetItemIdByItemName(ctx, v.Currency)
			if err != nil {
				return
			}
			tqtCurrencyItem = TqtItem{
				ItemId:   itemInfo.Id,
				ItemName: itemInfo.Name,
			}
			payType = SvrGameCoin
		}
		svrActivityStore = append(svrActivityStore, SvrActivityStoreContentV2{
			No: v.No,
			Price: TqtToSvrAward(TqtAward{
				TqtItem:   tqtCurrencyItem,
				ItemCount: currencyCount,
			}),
			BuyCountLimit: v.BuyCountLimit,
			IsDailyReset:  v.IsDailyReset,
			Award:         TqtToSvrAward(v.Award),
			PayType:       payType,
		})
	}
	return
}
